<template>
  <div class="el-input  wrapper-content">
    <input autocomplete="off" type="text" rows="2" validateevent="true" class="el-input__inner" disabled>
    <input type="file"
        :id="id"
        class="img"
        :accept="accept"
        @change.prevent="fileUpload($event)"
        :multiple="false"
        :disabled="disabled" />
    <div class="error-message">{{message}}</div>
    <div class="el-form-item__error">请输入数字</div>
  </div>
</template>

<script>
  export default {
    name: 'form-file',
    data: () => ({
      id: 'label',
      message: '',
      inputValue: '',
      fileData: '',
      imgDataList: {},
      disabled: false
    }),
    props: {
      accept: {
        type: String,
        default: 'image/*'
      },
      required: {
        type: Boolean,
        default: true
      },
      multiple: {
        type: String,
        default: ''
      }
    },
    methods: {
      fileUpload (e) {
        this.$emit('uploading')
        this.disabled = true
        let file = e.target.files[0]
        if (file) {
          let imgValue = file.name
          let len = imgValue.length
          if (len > 10) {
            this.inputValue = imgValue.substring(0, 18) + '...'
          } else {
            this.inputValue = imgValue
          }
          this.fileData = file
          this.message = ''
        } else {
          this.inputValue = ''
          this.message = '请上传文件'
        }
        // toast({content: '上传成功！'})
        this.$emit('upload-end')
        this.disabled = false
      }
    }
  }
</script>

<style lang="css" scoped>
  .el-form-item__error {
    display: none;
  }
  .wrapper-content {
    position: relative;
  }
  .wrapper-content::after {
    position: absolute;
    content: '附件';
    top: 1px;
    bottom: 0;
    right: 1px;
    width: 64px;
    height: 34px;
    line-height: 34px;
    background: rgb(191, 203, 217);
    text-align: center;
    border-left: 1px solid #ccc;
  }
  .img {
    position: absolute;
    top: 0px;
    bottom: 0px;
    width: 60px;
    right: 0px;
    z-index: 900;
    opacity: 0;
    cursor: pointer;
  }
  .wrapper-content:active::after {
    background-color: #ccc;
  }
</style>
